package org.iwidget.desktop.model;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JComponent;
import org.w3c.dom.*;
/**
 *
 * @author Muhammad Hakim A
 */

public class ElementTextField extends ElementUI
{
    class ScrollThread extends Thread
    {

        public void restart()
        {
            scrollPosition = 0;
        }

        @Override
        public void run()
        {
            for(boolean ended = false; scrollDirection != 0 && width <= calculateTextWidth() && !ended;)
            {
                if(scrollDirection == -1)
                {
                    scrollPosition--;
                    if(scrollPosition < -calculateTextWidth())
                        scrollPosition = width;
                } else
                if(scrollDirection == -1)
                {
                    if(scrollPosition > width - calculateTextWidth())
                        scrollPosition--;
                    else
                        ended = true;
                } else
                if(scrollDirection == 1)
                {
                    scrollPosition++;
                    if(scrollPosition > width)
                        scrollPosition -= calculateTextWidth();
                } else
                if(scrollDirection == 2)
                {
                    if(scrollPosition < 0)
                        scrollPosition++;
                    else
                        ended = true;
                    if(scrollPosition > width)
                        scrollPosition -= calculateTextWidth();
                }
                if(widget.getUIComponent() != null)
                    widget.getUIComponent().repaint();
                try
                {
                    Thread.sleep(speed);
                }
                catch(Exception e) { }
            }

        }

        private int speed;

        public ScrollThread(int speed)
        {
            this.speed = speed;
        }
    }


    public ElementTextField()
    {
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        font = "Helvetica";
        rotation = 0.0D;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        name = "Text";
    }

    public ElementTextField(WidgetElement widget)
    {
        super(widget);
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        font = "Helvetica";
        rotation = 0.0D;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        name = "Text";
    }

    public ElementTextField(WidgetElement widget, String name)
    {
        super(widget);
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        font = "Helvetica";
        rotation = 0.0D;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        this.name = name;
    }

    public String getAlignment()
    {
        return alignment;
    }

    public void setAlignment(String s)
    {
        if(alignment == null || !alignment.equals(s))
        {
            setChanged();
            alignment = s;
        }
    }

    public String getBgColor()
    {
        return bgColor;
    }

    public void setBgColor(String s)
    {
        if(bgColor == null || !bgColor.equals(s))
        {
            bgColor = s;
            setChanged();
        }
    }

    public int getBgOpacity()
    {
        return bgOpacity;
    }

    public void setBgOpacity(int i)
    {
        if(bgOpacity != i)
        {
            bgOpacity = i;
            if(bgOpacity > 100)
                bgOpacity = 100;
            if(bgOpacity < 0)
                bgOpacity = 0;
            setChanged();
        }
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String s)
    {
        if(color == null || !color.equals(s))
        {
            color = s;
            setChanged();
        }
    }

    public String getData()
    {
        return data;
    }

    public void setData(String s)
    {
        if(data == null || !data.equals(s))
        {
            data = s;
            rebuildFont();
            width = calculateTextWidth();
            height = calculateTextHeight();
            if(myComponent != null)
            {
                myComponent.setSize(width, height);
                if(scrollDirection != 0 && width <= calculateTextWidth())
                {
                    if(t != null && t.isAlive())
                        t.stop();
                    t = new ScrollThread(scrollSpeed);
                    t.setPriority(1);
                    t.start();
                }
            }
            setChanged();
        }
    }

    public String getFont()
    {
        return font;
    }

    public void setFont(String s)
    {
        if(font == null || !font.equals(s))
        {
            font = s;
            rebuildFont();
            width = calculateTextWidth();
            height = calculateTextHeight();
            setChanged();
        }
    }

    public double getRotation()
    {
        return rotation;
    }

    public void setRotation(double d)
    {
        if(rotation != d)
        {
            rotation = d;
            setChanged();
        }
    }

    public String getScrolling()
    {
        if(scrollDirection == -2)
            return "left";
        if(scrollDirection == -1)
            return "autoleft";
        if(scrollDirection == 1)
            return "autoright";
        if(scrollDirection == 2)
            return "right";
        else
            return "off";
    }

    public void setScrolling(String dir)
    {
        if(dir.equals("off"))
            scrollDirection = 0;
        else
        if(dir.equals("left"))
        {
            scrollDirection = -2;
            scrollPosition = 0;
        } else
        if(dir.equals("autoleft"))
        {
            scrollDirection = -1;
            scrollPosition = 0;
        } else
        if(dir.equals("right"))
        {
            scrollDirection = 2;
            scrollPosition = -calculateTextWidth() - width;
        } else
        if(dir.equals("autoright"))
        {
            scrollDirection = 1;
            scrollPosition = -calculateTextWidth() - width;
        }
    }

    public int getScrollDirection()
    {
        return scrollDirection;
    }

    public void setScrollDirection(int i)
    {
        scrollDirection = i;
    }

    public int getScrollPosition()
    {
        return scrollPosition;
    }

    public void setScrollPosition(int i)
    {
        scrollPosition = i;
    }

    public int getScrollSpeed()
    {
        return scrollSpeed;
    }

    public void setScrollSpeed(int i)
    {
        scrollSpeed = i;
    }

    public int getSize()
    {
        return size;
    }

    public void setSize(int i)
    {
        if(size != i)
        {
            size = i;
            rebuildFont();
            width = calculateTextWidth();
            height = calculateTextHeight();
            setChanged();
        }
    }

    public String getStyle()
    {
        return style;
    }

    public void setStyle(String s)
    {
        if(style == null || !style.equals(s))
        {
            style = s;
            rebuildFont();
            width = calculateTextWidth();
            height = calculateTextHeight();
            setChanged();
        }
    }

    public Font getFontObject()
    {
        return fontObject;
    }

    public FontMetrics getFontMetrics()
    {
        return fm;
    }

    public void rebuildFont()
    {
        int styleInt = 0;
        if(style.toLowerCase().equals("bold"))
            styleInt = 1;
        else
        if(style.toLowerCase().equals("italic"))
            styleInt = 2;
        fontObject = new Font(font, styleInt, size);
        fm = widget.getParent().getFontMetrics(fontObject);
    }

    public int calculateTextWidth()
    {
        if(fontObject == null || fm == null)
            rebuildFont();
        if(data != null && fm != null)
        {
            int result = fm.stringWidth(data) + 2;
            return result;
        } else
        {
            return 0;
        }
    }

    public int calculateTextHeight()
    {
        rebuildFont();
        if(fm != null)
            return fm.getHeight() + fm.getDescent();
        else
            return 0;
    }

    public void parse(Node textNode)
        throws Exception
    {
        NamedNodeMap map = textNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue(), null);
        }

        NodeList nodes = textNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue(), childNode);
        }

        if(getName() == null || getName().length() == 0)
        {
            widget.getParent().getDebugFrame().addText("SYSTEM:Error:Text element is not named");
            setName("Unknown_" + System.currentTimeMillis());
        }
    }

    private void parseHelper(String nodeName, String value, Node childNode)
    {
        if(nodeName.equals("alignment"))
            setAlignment(value);
        else
        if(nodeName.equals("bgColor"))
            setBgColor(value);
        else
        if(nodeName.equals("bgOpacity"))
            setBgOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("color"))
            setColor(value);
        else
        if(nodeName.equals("cursor"))
            setCursor(value);
        else
        if(nodeName.equals("data"))
            setData(value);
        else
        if(nodeName.equals("font"))
            setFont(value);
        else
        if(nodeName.equals("height"))
            setHeight(Integer.parseInt(value));
        else
        if(nodeName.equals("name"))
            setName(value);
        else
        if(nodeName.equals("opacity"))
            setOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("owner"))
            setOwner(value);
        else
        if(nodeName.equals("rotation"))
            setRotation(Double.parseDouble(value));
        else
        if(nodeName.equals("scrolling"))
            setScrolling(value);
        else
        if(nodeName.equals("scrollSpeed"))
            setScrollSpeed(Integer.parseInt(value));
        else
        if(nodeName.equals("size"))
            setSize(Integer.parseInt(value));
        else
        if(nodeName.equals("string"))
            setData(value);
        else
        if(nodeName.equals("style"))
            setStyle(value);
        else
        if(nodeName.equals("tooltip"))
            setTooltip(value);
        else
        if(nodeName.equals("visible"))
        {
            if(value.equals("true"))
                setVisible(true);
            else
                setVisible(false);
        } else
        if(nodeName.equals("width"))
            setWidth(Integer.parseInt(value));
        else
        if(nodeName.equals("x"))
            setX(Integer.parseInt(value));
        else
        if(nodeName.equals("y"))
            setY(Integer.parseInt(value));
        else
        if(nodeName.startsWith("on"))
            addAction(nodeName, addChildAction(nodeName, childNode));
    }

    public void setUIComponent(JComponent myComponent)
    {
        super.setUIComponent(myComponent);
        rebuildFont();
        if(scrollDirection != 0 && width <= calculateTextWidth())
        {
            if(t != null && t.isAlive())
            {
                t.stop();
                t = null;
            }
            t = new ScrollThread(scrollSpeed);
            t.setPriority(1);
            t.start();
        }
    }

    public static String ALIGN_LEFT = "left";
    public static String ALIGN_CENTER = "center";
    public static String ALIGN_RIGHT = "right";
    private String alignment;
    private String bgColor;
    private int bgOpacity;
    private String color;
    private String data;
    private String font;
    private double rotation;
    private int scrollDirection;
    private int scrollPosition;
    private int scrollSpeed;
    private int size;
    private String style;
    private Font fontObject;
    private FontMetrics fm;
    public ScrollThread t;

}